import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.Date;

public class ServletCookie extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie[] cookies=request.getCookies();
        int len=0,index=0;
        boolean tag=false;
        if(cookies!=null) len=cookies.length;
        for (int i = 0; i < len; i++) {
            if(cookies[i].getName().equals("lastTime")){
                index=i;
                tag=true;
                break;
            }
        }
        response.setContentType("text/html;charset=utf-8");
        if(tag){//找到了
            response.getWriter().write("你上一次访问我的网站的时间是："+cookies[index].getValue());
        }else{
            response.getWriter().write("欢迎你，第一次来到我的网站！");
        }
        Date now=new Date();
        String nowString=now.toString();
        nowString=nowString.replace(" ","@");
        System.out.println(nowString);
        Cookie cookie=new Cookie("lastTime",nowString);
        cookie.setMaxAge(60);
        response.addCookie(cookie);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
